package com.tanhua.server.service;

import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.dubbo.api.db.BlackListApi;
import com.tanhua.dubbo.api.db.QuestionApi;
import com.tanhua.dubbo.api.db.SettingApi;
import com.tanhua.server.interceptors.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class SettingService {
    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    private SettingApi settingApi;
    @DubboReference
    private BlackListApi blackListApi;

    public ResponseEntity querySettings() {
        SettingsVo settingsVo = new SettingsVo();

        User user = UserHolder.getUser();
        settingsVo.setId(user.getId());
        settingsVo.setPhone(user.getMobile());

        Question question = questionApi.findByUserId(user.getId());
        settingsVo.setStrangerQuestion( question==null?"你喜欢高山还是大海?":question.getTxt()   ); //陌生人问题

        Settings settings = settingApi.findByUserId(user.getId());
        if(settings!=null){
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
        }
        return ResponseEntity.ok(settingsVo);
    }

    public ResponseEntity saveQuestions(String content) {
        User user = UserHolder.getUser();
//        根据userId查询Question，
        Question question = questionApi.findByUserId(user.getId());
        if(question==null){//        如果查不到就应该新增
            question = new Question();
            question.setUserId(user.getId());
            question.setTxt(content);
            questionApi.save(question);
        }else{  //        如果能查到就应该修改
            question.setTxt(content);
            questionApi.update(question);
        }
        return ResponseEntity.ok(null);
    }

    public ResponseEntity saveSettings(Settings settings) {

//        根据userId 查询setting
        Settings settingsDb = settingApi.findByUserId(UserHolder.getUserId());
        if(settingsDb==null){ //如果查不到应该新增
            settings.setUserId(UserHolder.getUserId());
            settingApi.save(settings);
        }else{ // 如果查到了应该修改
            settings.setId(settingsDb.getId());
            settingApi.update(settings); //updateById
        }
        return ResponseEntity.ok(null);
    }

    public ResponseEntity queryBlacklist(Integer page, Integer pagesize) {
      PageResult pageResult = blackListApi.queryBlacklist(UserHolder.getUserId(),page,pagesize);
      return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity deleteBlacklist(Long blackUserId) {
        blackListApi.deleteBlacklist(UserHolder.getUserId(),blackUserId);
        return ResponseEntity.ok(null);
    }
}
